import Cookies from "js-cookie";
import $store from '@/store'
const FlutterBridge = {};

/** 
* @Description: RequestFlutterBridge 方法说明
method 方法名: 
* showLoading 显示 loading 
* closeLoading 关闭 loading 
* getTokenFromFlutter 获取token
*/

/**
* @Description: FlutterBridge flutter通信请求
* @Author: 王燕欣
* @Date: 2024-10-12 13:11
*/
FlutterBridge.RequestFlutterBridge = (methodName, params) => {
    // console.log('method----->', methodName)
    // 调试  flutter通信
    // console.log('window.navigator---000---', window.navigator);
    // console.log('window.navigator.userAgent----11111', window.navigator.userAgent);
    // console.log('window.navigator.userAgent.includes("FlutterBridge")----2222', window.navigator.userAgent.includes("FlutterBridge"));

    let paramsJsonStr = JSON.stringify({
        "method": methodName, // 方法名
        data: params, // 
    })
    console.log('RequestFlutterBridge-----paramsJsonStr------', paramsJsonStr)
    new Promise((resolve, reject) => {
        try {
            if (window.navigator.userAgent.includes("FlutterBridge")) {
                window.FlutterBridge.postMessage(
                    paramsJsonStr
                );
                // console.log("flutter通信---成功---");
                return resolve()
            } else {
                console.warn("flutter通信--暂无----FlutterBridge----方法--");
                return
            }
        } catch (error) {
            console.error("flutter通信---错误--->", error);
            return reject(error)
        }
    });
}
/**
* @Description: flutter 通信回掉方法，flutter调用h5方法;
* @Author: 王燕欣
* @Date: 2024-10-12 13:58
*/

FlutterBridge.FlutterBridgeCallback = (res) => {
    console.log("flutter 回掉方法--callback-res-0000-", res);
    // 判断是否是json字符串 如果不是直接抛出错误
    if (!isValidJSON(res)) {
        console.error("flutter通信--callback-错误--1->", res);
        return;
    }
    // callback 参数 json解析
    let callbackParams = JSON.parse(res);

    console.log("flutter 回掉方法---callbackParams-111111-", callbackParams);

    try {
        switch (callbackParams.method) {
            case "getTokenFromFlutter": // 获取token方法
                getTokenFromFlutter(callbackParams.data);
                break;
            case "rightSideSearch": // 右上角搜索事件 
                rightSideSearchFun(callbackParams);
                break;
            default:
                break;
        }
    } catch (error) {
        console.error("flutter通信--callback-错误-2-->", error);
    }
}
/**
* @Description: 处理token 方法
* @Author: 王燕欣
* @Date: 2024-10-12 14:02
*/

export function getTokenFromFlutter(params) {
    console.log('--处理token 方法------>', params);
    console.log('--处理token 方法------>', params.token);
    let tokenVal = params.token;
    $store.commit('SET_TOKEN', params.token);
    Cookies.set("TokenHead", tokenVal.split(' ')[0] + ' ');
    Cookies.set("Token", tokenVal.split(' ')[1]);
    // Cookies.set("ExpiresIn", resData.expiresIn); // 过期时间
}

export function rightSideSearchFun(params) {
    console.log('右上角搜索 方法--rightSideSearchFun---params--->', params);

    switch (params.type) {
        case "gis":
            // gis 监控
            $store.commit('RIGHT_SIDE_SEARCH_GIS', params);
            break;
        case "huifang":
            // 回放
            $store.commit('RIGHT_SIDE_SEARCH_HISTORY_TRACK', params);
            break;
        case "zhibiao":
            // 指标搜索
            $store.commit('RIGHT_SIDE_SEARCH_INDICATOR', params);
            break;
        default:
            break;
    }
}
/**
* @Description: 检查是否是 json 字符串
* @Author: 王燕欣
* @Date: 2024-10-12 13:53
*/
export function isValidJSON(str) {
    try {
        JSON.parse(str);
        return true;
    } catch (e) {
        return false;
    }
}

export default FlutterBridge;